<template>
  <div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="checkDialog">确 认</el-button>
    </div>

    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>

    <FilterTable
      ref="table"
      columnsRef="xmzVehicleProductModel"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :isPagination="true"
      :onColumnsChange="columns => (this.columns = columns)"
      :isSelectColumn="true"
      :onSyncTableData="tableHandle"
      selectionType="single"
      :onRowSelectChange="selectChangeHandle"
      height="auto"
      maxHeight="400"
    >
      <template slot="controls" slot-scope="props"></template>
    </FilterTable>
  </div>
</template>
<script>
// @Author:徐铭泽

import { confirmAction, isFormEmpty } from '@/utils';
import { getCar } from '@/api/common/cars/masterDataCars';

export default {
  name: '',
  data() {
    this.selectedRow = null;
    return {
      topFilterList: this.createTopFilters(),
      fetchApi: getCar,
      columns: this.createTableColumns(),
      fetchParams: {},
      tableData: [] // 表单数据
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table;
  },
  methods: {
    selectChangeHandle(rows) {
      if (!rows.length) {
        this.selectedRow = null;
      } else {
        this.selectedRow = rows[0];
      }
    },
    createTopFilters() {
      return [
        {
          type: 'INPUT',
          label: '产品代码',
          fieldName: 'productCode',
          maxlength: 15
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '产品代码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          minWidth: 150
        },
        {
          title: '产品名称',
          dataIndex: 'productName',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        }
      ];
    },

    async checkDialog() {
      if (isFormEmpty(this.selectedRow)) {
        try {
          await confirmAction('请至少选择一条数据，再进行操作');
          return;
        } catch (err) {}
      } else {
        this.$emit('setProductInfo', this.selectedRow);
        this.closeDialog('close');
      }
    },
    // 搜索
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    },
    // table动态回调函数
    tableHandle(val) {
      this.tableData = val;
    }
  }
};
</script>

<style lang="less" scoped></style>
